<?php
/**
 *  CMS 频道管理
 *  仲伟涛 2011-4-24
 */
class Admin_Page_Cms_Class extends Admin_Page_Abstract
{
    public function validate(ME_Request $input, ME_Response $output)
    {
        $input->doJump = true;
        $adminInfo = Libs_UcUser::getAdminLoginInfo($input,$output);
        $output->appId = 
        $input->appId = (int)$input->get("appId");

        return true;
    }

    private $db;
    private $tblName = 'cms_class';
    private function loadDb()
    {
        $this->db = Db_Admin::instance();
    }


    public function doDefault(ME_Request $input, ME_Response $output)
    {
        $page = (int)$input->get("page");
        $page = max(1,$page);
        $this->loadDb();
        //获得所有记录
        $param = array(
                'db'          => $this->db,
                'fromSql'     => " from {$this->tblName} ", #from部分的语句
                'orderBySql'  => ' order by id desc ',
                'whereSql'  => " where appId = {$input->appId} ",
                'pagesize'  => 20,
                'page'      => $page,
                'urlTpl'    => 'index.php?c=Cms_Class&page={PAGE}',
        );
        $listData = Libs_DBHelper::getList($param);
        $output->pageBar = $listData['pageBar'];
        $output->list = $listData['list'];
        #顶部
        $output->headerHtml = Admin_Plugin_Common_Tpl::getHeader($input, $output);
        //APP缓存
        $output->appCache = $this->loadCache("Global_App",array());
        $output->setTemplate("Cms/ClassList");
    }
    public function doAddShow(ME_Request $input, ME_Response $output)
    {
        $id = (int)$input->get("id");
        if($id){
            $this->loadDb();
            $sql = "select * from {$this->tblName} where id=".$id;
            $output->dataInfo = $this->db->getRow($sql);
            $output->id = $id;
        }

        $output->appCache = $this->loadCache("Global_App",array());
        #顶部
        $output->headerHtml = Admin_Plugin_Common_Tpl::getHeader($input, $output);
        $output->setTemplate("Cms/ClassAdd");
    }
    public function doAdd(ME_Request $input, ME_Response $output)
    {        
        $this->loadDb();
        $id = (int)$input->post("id");
        $item = $input->post("item");
        $options = array(
                'db'        => $this->db,
                'tableName' => $this->tblName,
                'addItem'   => $item,
                'debug'   => 1,
            );
        if($id){//编辑
            $options['where'] = ' where id = '.$id;
            Libs_DBHelper::updateItem($options);
        }else{
            Libs_DBHelper::insertItem($options);
        }
        echo "<script>window.location.href='index.php?c=Cms_Class'</script>";
        exit;
    }
    public function doMultPublish(ME_Request $input, ME_Response $output)
    {
        $classIds = $input->post("classIds");
        $appId    = $input->post("appId");
        if($classIds){
            foreach($classIds as $cid){
                $rtn = Libs_Cms_Create::publishClass(array(
                                                            'output'  => $output,
                                                            'appId'   => $appId,
                                                            'classId' => $cid,
                                                        ));
                echo $rtn['name'] . " <a href='" . $rtn['url'] . "' target='_blank'>" . $rtn['url'] . "</a><br/>";
            }
        }

        exit;
    }

}
